@extends('layout.master')

@section('title','发送帖子')

@section('css')
    @parent
    <!-- animation CSS -->
    <link href="{{ asset('css/animate.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/icheck/skins/all.css') }}" rel="stylesheet">
    <link href="{{ asset('plugins/bower_components/bootstrap-switch/bootstrap-switch.min.css') }}" rel="stylesheet">
@endsection
@section('main')
    <div class="row">
        <div class="col-lg-12 col-md-12">
            <div class="white-box">
                <form class="form-horizontal form-material" method="post" id="tiezi"
                      enctype="multipart/form-data">
                    <div class="row">
                        <div class="col-md-6 col-sm-6">
                            <div class="form-group">
                                <label class="col-md-12">发送时间<span
                                            style="color:red;font-size: smaller">*即时发送填1</span></label>
                                <div class="col-md-12">
                                    <input type="text" name="view_time" id="view-time"
                                           placeholder="2019-01-15 12:12:12"
                                           class="form-control form-control-line">
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6 col-sm-6">
                            <div class="form-group">
                                <label class="col-md-12">加精时间<span
                                            style="color:red;font-size: smaller">*即时加精填1,不填即不加精</span></label>
                                <div class="col-md-12">
                                    <input type="text" name="hot_time" id="hot-time"
                                           placeholder="2019-01-15 12:12:12"
                                           class="form-control form-control-line">
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-3 col-md-3">
                            <label class="control-label">选择用户</label>
                            <div class="example">
                                <select style="height: 38px" name="user_id" id="user-id">
                                    @foreach(App\User::PostingsUsers() as $value)
                                        <option value="{{ $value->c_id }}">{{ $value->name }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="col-md-3 col-sm-6">
                            <div class="form-group">
                                <label class="col-md-12">标题</label>
                                <div class="col-md-12">
                                    <input type="text" name="title" id="title"
                                           class="form-control form-control-line">
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-3 col-md-3">
                            <label class="control-label">科室</label>
                            <div class="example">
                                <select style="height: 38px" name="keshi_id" id="keshi-id">
                                    @foreach(App\Keshi::Keshis() as $value)
                                        <option value="{{ $value->id }}">{{ $value->title }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                        <div class="col-lg-3 col-md-3">
                            <label class="control-label">话题</label>
                            <div class="example">
                                <select style="height: 38px" name="huati_id" id="huati-id">
                                    <option value="">无</option>
                                    @foreach(App\Huati::Huatis() as $value)
                                        <option value="{{ $value->id }}">{{ $value->title }}</option>
                                    @endforeach
                                </select>
                            </div>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-sm-12">
                            <textarea style="height: 150px; width: 100%;overflow:auto;" name="contents"
                                      id="contents"></textarea>
                        </div>
                    </div>
                    <div class="row">
                        <div class="col-lg-2 col-md-2">
                            <div class="row">
                                <label class="control-label">图片/视频</label>
                            </div>
                            <div class="row">
                                <div class="radio radio-purple">
                                    <input type="radio" name="porm" checked id="pic" value="1">
                                    <label for="radio8"> 图片 </label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="radio radio-purple">
                                    <input type="radio" name="porm" id="video" value="2">
                                    <label for="radio8"> 视频 </label>
                                </div>
                            </div>
                        </div>
                        <div class="col-lg-9 col-md-9">
                            <div class="row">
                                <div class="col-lg-4 col-md-4" id="file1">
                                    <div class="form-group ">
                                        <input type="file" id="file-1" class="dropify"
                                               name="file1"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file2">
                                    <div class="form-group ">
                                        <input type="file" id="file-2" class="dropify" name="file2"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file3">
                                    <div class="form-group ">
                                        <input type="file" id="file-3" class="dropify" name="file3"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file4">
                                    <div class="form-group ">
                                        <input type="file" id="file-4" class="dropify" name="file4"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file5">
                                    <div class="form-group ">
                                        <input type="file" id="file-5" class="dropify" name="file5"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file6">
                                    <div class="form-group ">
                                        <input type="file" id="file-6" class="dropify" name="file6"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file7">
                                    <div class="form-group ">
                                        <input type="file" id="file-7" class="dropify" name="file7"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file8">
                                    <div class="form-group ">
                                        <input type="file" id="file-8" class="dropify" name="file8"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                                <div class="col-lg-4 col-md-4" id="file9">
                                    <div class="form-group ">
                                        <input type="file" id="file-9" class="dropify" name="file9"
                                               data-height="100px"
                                               data-default-file=""/>
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>
                    <div class="form-group">
                        <div class="row">
                            <button class="fcbtn btn btn-success btn-outline btn-1e waves-effect" id="posting">发帖
                            </button>
                            {!! csrf_field() !!}
                        </div>
                    </div>

                </form>
            </div>
        </div>
    </div>
@endsection
@section('js')
    @parent
    <script src="{{ asset('js/myself.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup.min.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup-init.js') }}"></script>
    <script type="text/javascript">

        $('#pic').click(function () {
            $('#file2').show();
            $('#file3').show();
            $('#file4').show();
            $('#file5').show();
            $('#file6').show();
            $('#file7').show();
            $('#file8').show();
            $('#file9').show();
        });

        $('#video').click(function () {
            $('#file2').hide();
            $('#file3').hide();
            $('#file4').hide();
            $('#file5').hide();
            $('#file6').hide();
            $('#file7').hide();
            $('#file8').hide();
            $('#file9').hide();
        });

        $("#posting").click(function (e) {
            e.preventDefault();
            var view_time = $("#view-time").val();
            var hot_time = $("#hot-time").val();
            var user_id = $("#user-id").val();
            var title = $("#title").val();
            var keshi_id = $("#keshi-id").val();
            var huati_id = $("#huati-id").val();
            var contents = $("#contents").val();
            var porm = $('input[name="porm"]:checked').val();
            var file1 = document.getElementById("file-1").files;
            var file2 = document.getElementById("file-2").files;
            var file3 = document.getElementById("file-3").files;
            var file4 = document.getElementById("file-4").files;
            var file5 = document.getElementById("file-5").files;
            var file6 = document.getElementById("file-6").files;
            var file7 = document.getElementById("file-7").files;
            var file8 = document.getElementById("file-8").files;
            var file9 = document.getElementById("file-9").files;

            var data = new FormData();
            data.append('view_time', view_time);
            data.append('hot_time', hot_time);
            data.append('user_id', user_id);
            data.append('title', title);
            data.append('keshi_id', keshi_id);
            data.append('huati_id', huati_id);
            data.append('contents', contents);
            data.append('porm', porm);

            if (file1.length) {
                data.append('file1', file1[0]);
            }
            if (file2.length && ((file2[0].type).indexOf("image/") !== -1)) {
                data.append('file2', file2[0]);
            }
            if (file3.length && ((file3[0].type).indexOf("image/") !== -1)) {
                data.append('file3', file3[0]);
            }
            if (file4.length && ((file4[0].type).indexOf("image/") !== -1)) {
                data.append('file4', file4[0]);
            }
            if (file5.length && ((file5[0].type).indexOf("image/") !== -1)) {
                data.append('file5', file5[0]);
            }
            if (file6.length && ((file6[0].type).indexOf("image/") !== -1)) {
                data.append('file6', file6[0]);
            }
            if (file7.length && ((file7[0].type).indexOf("image/") !== -1)) {
                data.append('file7', file7[0]);
            }
            if (file8.length && ((file8[0].type).indexOf("image/") !== -1)) {
                data.append('file8', file8[0]);
            }
            if (file9.length && ((file9[0].type).indexOf("image/") !== -1)) {
                data.append('file9', file9[0]);
            }


            swal({
                    title: "",
                    text: "确定发送么？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({

                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        url: "/postings/post/timing",
                        type: "POST",
                        data: data,
                        cache: false,
                        contentType: false,
                        async: false,
                        processData: false,
                        error: function (e) {
                            if (e.readyState === 4 && e.status === 422) {
                                for (var i in e.responseJSON) {
                                    if (e.responseJSON[i] !== 'The given data was invalid.') {
                                        err = e.responseJSON[i];
                                        for (var j in err) {
                                            toasterror(err[j][0]);
                                        }
                                    }
                                }
                            }
                        },

                        success: function (data) {
                            swal(data.message, '', 'success');
                            $('.confirm').click(function () {
                                location.reload();
                            });
                        }
                    })
                }
            );
        });

    </script>
@endsection